package com.mogist.hqc.module.product.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.support.v7.widget.LinearLayoutManager
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import butterknife.OnClick
import butterknife.Optional
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.entitys.BaseModel
import com.mogist.hqc.entitys.response.OrderVo
import com.mogist.hqc.entitys.response.UploadVo
import com.mogist.hqc.module.common.ui.WebActivity
import com.mogist.hqc.module.order.ui.OrderPayActivity
import com.mogist.hqc.module.product.adapter.ProductOrderSureAdapter
import com.mogist.hqc.module.product.contract.ProductUploadContract
import com.mogist.hqc.module.product.presenter.ProductUploadPresenter
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.util.Constant
import kotlinx.android.synthetic.main.activity_product_upload.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import talex.zsw.basecore.util.RegTool
import talex.zsw.basecore.util.TextTool
import talex.zsw.basecore.view.other.RxToast
import talex.zsw.basecore.view.recyleview.DividerItemDecoration

/**
 * 作用：下单
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class ProductUploadActivity : BaseMVPActivity<ProductUploadContract.Presenter>(), ProductUploadContract.View
{
    var data: UploadVo? = null

    override fun initArgs(intent: Intent)
    {
        intent.getSerializableExtra("data")
                ?.let {
                    data = it as UploadVo
                }
    }

    override fun initView()
    {
        setContentView(R.layout.activity_product_upload)
        mPresenter = ProductUploadPresenter(this)
    }

    @SuppressLint("SetTextI18n")
    override fun initData()
    {
        mTvNotice.movementMethod = LinkMovementMethod.getInstance()
        mTvTitle.text = data?.pdtName
        mTvInfo.text = "套餐类型：${data?.proName}\n出行时间：${data?.travelDate}\n${data?.basePlace}出发"
        if (data!!.adultNum > 0)
        {
            mTvInfo.append("  ${data!!.adultNum}成人")
        }
        if (data!!.childNum > 0)
        {
            mTvInfo.append("  ${data!!.childNum}儿童")
        }
        mTvPrice.text = data?.price

        TextTool.getBuilder("点击\"提交订单\"，即表示已阅读并同意")
                .append("《好去处服务协议》")
                .setClickSpan(object : ClickableSpan()
                              {
                                  override fun onClick(widget: View)
                                  {
                                      // 《好去处服务协议》
                                      val intent = Intent(this@ProductUploadActivity, WebActivity::class.java)
                                      intent.putExtra("title", "好去处服务协议")
                                      intent.putExtra("data", "2")
                                      start(intent)
                                  }

                                  override fun updateDrawState(ds: TextPaint)
                                  {
                                      ds.color = Color.BLUE
                                      ds.isUnderlineText = false
                                  }
                              })
                .append("《旅游安全须知》")
                .setClickSpan(object : ClickableSpan()
                              {
                                  override fun onClick(widget: View)
                                  {
                                      // 《旅游安全须知》
                                      val intent = Intent(this@ProductUploadActivity, WebActivity::class.java)
                                      intent.putExtra("title", "旅游安全须知")
                                      intent.putExtra("data", "3")
                                      start(intent)
                                  }

                                  override fun updateDrawState(ds: TextPaint)
                                  {
                                      ds.color = Color.BLUE
                                      ds.isUnderlineText = false
                                  }
                              })
                .append("  ")
                .into(mTvNotice)

        initRecyclerView()
        sureAdapter.replaceData(data!!.list)
    }

    @Optional
    @OnClick(R.id.mTvPrice, R.id.mTvSure, R.id.mTvMask)
    fun onViewClicked(view: View)
    {
        if (!isFastClick)
        {
            when (view.id)
            {
                R.id.mTvMask  ->
                {
                    mLLOrder.visibility = View.GONE
                }
                R.id.mTvPrice ->
                {
                    if (mLLOrder.visibility == View.GONE)
                    {
                        mLLOrder.visibility = View.VISIBLE
                    }
                    else
                    {
                        mLLOrder.visibility = View.GONE
                    }
                }
                R.id.mTvSure  ->
                {
                    val name =
                            mEtName.text.toString()
                                    .trim()
                    val phone =
                            mEtPhone.text.toString()
                                    .trim()
                    val email =
                            mEtEmail.text.toString()
                                    .trim()
                    val mark =
                            mEtMark.text.toString()
                                    .trim()
                    if (RegTool.isEmpty(name))
                    {
                        RxToast.error("请输入联系人姓名")
                    }
                    else if (RegTool.isEmpty(phone) || !RegTool.isMobileSimple(phone))
                    {
                        RxToast.error("请输入正确的联系人电话")
                    }
                    else if (!RegTool.isEmpty(email) && !RegTool.isEmail(email))
                    {
                        RxToast.error("请输入正确的邮箱地址")
                    }
                    else
                    {
                        // 提交订单
                        val body = BaseModel(true)
                        body.pdtName = data?.pdtName
                        body.price = data?.price
                        body.travelDate = data?.travelDate
                        body.adultNum = data!!.adultNum.toString()
                        body.childNum = data!!.childNum.toString()
                        body.pdtId = data?.pdtId
                        body.proId = data?.proId
                        body.linkName = name
                        body.email = email
                        body.mobile = phone
                        body.remark = mark
                        mPresenter.getData(HttpDto(Constant.ORDER_UPLOAD,
                                                   body).setErrorTitle("下单失败"))
                    }
                }
            }
        }
    }

    private val sureAdapter = ProductOrderSureAdapter()
    private fun initRecyclerView()
    {
        mRvOrder.addItemDecoration(DividerItemDecoration(DividerItemDecoration.VERTICAL_LIST,
                                                         R.color.transparent))
        mRvOrder.layoutManager = LinearLayoutManager(this@ProductUploadActivity)
        mRvOrder.adapter = sureAdapter
    }

    override fun bindOrder(data: OrderVo)
    {
        val intent = Intent(this@ProductUploadActivity, OrderPayActivity::class.java)
        intent.putExtra("order",data)
        start(intent)
        EventBus.getDefault().post(Constant.EVENT_UPLOAD_SUCCESS)
        finish()
    }

    // --------------- EventBus ---------------
    @Subscribe
    fun onEvent(event: String)
    {
        if (Constant.EVENT_PAY_SUCCESS == event)
        {
            finish()
        }
    }
}